/**
 * Project: sms_toy
 * File: T2SSwitchWgt.java
 * Date: 2012-3-2 2012
 * Author: Awang
 * Copyright (c) 2012 Alex Wang
 */

package com.joyo8.app;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.RemoteViews;

/**
 * @author a.w@qq.com
 */
public class T2SSwitchWgt extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Constants.mUtil.setSpeechSMS(context, false);
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        super.onDisabled(context);
        Constants.mUtil.setSpeechSMS(context, false);
    }

    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);
        Constants.mUtil.setSpeechSMS(context, true);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
        String _action = intent.getAction();
        if(Constants.INTENT_UPDATE_SPEECH.equals(_action)){
            boolean state = Constants.mUtil.getSpeechSMSState(context);
            Constants.mUtil.logd("speech state(old): "+state);
            Constants.mUtil.setSpeechSMS(context, !state);
            // to get wiget pic
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.t2s_wgt);
            if(state){
                rv.setImageViewResource(R.id.img_t2s_state, R.drawable.check_off);
            }else{
                rv.setImageViewResource(R.id.img_t2s_state, R.drawable.check_on);
            }
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            appWidgetManager.updateAppWidget(new ComponentName(context, T2SSwitchWgt.class), rv);
            
            Constants.mUtil.logd("speech state(new): "+Constants.mUtil.getSpeechSMSState(context));
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Constants.mUtil.logd("this is [" + appWidgetId + "] onUpdate!");
            Constants.mUtil.setSpeechSMS(context, true);
        }
        Intent intent = new Intent(Constants.INTENT_UPDATE_SPEECH);
        PendingIntent pending= PendingIntent.getBroadcast(context, 0, intent, 0);
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.t2s_wgt);
        rv.setOnClickPendingIntent(R.id.img_t2s_state, pending);
        
        appWidgetManager.updateAppWidget(appWidgetIds, rv);
    }
    
    
    

}
